package com.hs.srm.admin.common.util;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hs.srm.admin.modules.orders.entity.ScmPoItemEntity;
import com.hs.srm.admin.modules.orders.service.ScmPoItemService;
import com.hs.srm.admin.modules.requisition.entity.NproPurchRequestHeaderEntity;
import com.hs.srm.admin.modules.requisition.service.NproPurchRequestHeaderService;
import com.hs.srm.base.modules.sys.entity.SysUserEntity;
import com.hs.srm.common.utils.DozerUtil;
import com.landray.kmss.km.review.webservice.Exception_Exception;
import com.landray.kmss.km.review.webservice.IKmReviewWebserviceService;
import com.landray.kmss.km.review.webservice.KmReviewParamterForm;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.servlet.annotation.WebServlet;
import java.util.Optional;

/**
 * OA发送通知工具类
 *
 * @author ych
 * @date 2019年11月4日08:59:19
 */
@Controller
public class OANoticeUtil {

    @Autowired
    NproPurchRequestHeaderService nproPurchRequestHeaderServiceImpl;

    public static NproPurchRequestHeaderService nproPurchRequestHeaderService;

    @Autowired
    ScmPoItemService scmPoItemServiceImpl;

    private static ScmPoItemService scmPoItemService;


    @PostConstruct
    public void init() {
        nproPurchRequestHeaderService = nproPurchRequestHeaderServiceImpl;
        scmPoItemService = scmPoItemServiceImpl;
    }

    /**
     * @Description 根据采购订单号查询对应的申请单
     * @Param [订单号, 订单行项目]
     * @Return
     * @Author socik
     * @Date 2019/11/4 10:11
     */

    public static ScmPoItemEntity selectCodeByPoItem(String PoNo, String ItemNo) {
        ScmPoItemEntity scmPoItemEntity = scmPoItemService.selectByPoItem(PoNo, ItemNo);
        return scmPoItemEntity;
    }


    /**
     * OA通知下单人
     *
     * @param requestCode 请购单编码
     * @param content     通知内容
     * @param docSubject  文档标题
     * @return
     */
    public static boolean remindCreator(String requestCode, String content, String docSubject) throws Exception_Exception {
        return remind(requestCode, content, docSubject, false);
    }

    /**
     * @Description 修改输入方法
     * @Param [requestCode, content, docSubject, flg]
     * @Return boolean
     * @Author socik
     * @Date 2019/11/11 10:12
     */

    public static boolean remind(String requestCode, String content, String docSubject, boolean flg) throws Exception_Exception {
        //获取下单人OA账号
        NproPurchRequestHeaderEntity headerEntity = nproPurchRequestHeaderService.selectOne(
                new EntityWrapper<NproPurchRequestHeaderEntity>()
                        .eq("purch_request_code", requestCode));
        String OAccount = headerEntity.getOAccount();
        return sendMsg(OAccount, content, docSubject, flg);
    }

    /**
     * @Description 发送oa主要方法
     * @Param [OAccount, content, docSubject, flg]
     * @Return boolean
     * @Author socik
     * @Date 2019/11/11 10:13
     */

    public static boolean sendMsg(String OAccount, String content, String docSubject, boolean flg) throws Exception_Exception {


        String wsdlURL = "http://" + OAConfig.host + ":" + OAConfig.port
                + "/ekp/sys/webservice/kmReviewWebserviceService?wsdl";

        //调用OA流程启动接口
        IKmReviewWebserviceService service = (IKmReviewWebserviceService) callService(wsdlURL, IKmReviewWebserviceService.class);
        KmReviewParamterForm form = new KmReviewParamterForm();
        // 流程发起人
        String loginName = null;
        if (flg) {
            loginName = "HS_SRM";
        } else {
            SysUserEntity user = DozerUtil.map(SecurityUtils.getSubject().getPrincipal(), SysUserEntity.class);
            loginName = user.getOaAccount();
        }
        form.setDocCreator("{\"LoginName\":\"" + loginName + "\"}");
        // 文档标题
        form.setDocSubject(docSubject);

        // 文档模板id
        form.setFdTemplateId(OAConfig.noticeTemplateId);
        // 流程表单
        String formValues = "{\"fd_recipient\":\"" + OAccount + "\",\"fd_context\":\"" + content + "\"}";
        form.setFormValues(formValues);
        String str = service.addReview(form);
        return true;
    }

    /**
     * 检查OA账号是否为空
     *
     * @param requestCode
     * @return
     */
    public static String isOAccountBlank(String requestCode) {
        //当前用户
        SysUserEntity user = DozerUtil.map(SecurityUtils.getSubject().getPrincipal(), SysUserEntity.class);
        String loginName = user.getOaAccount();
        if (StringUtils.isBlank(loginName)) {
            return "OA账号为空，请添加OA账号！";
        }
        //获取下单人OA账号
        NproPurchRequestHeaderEntity headerEntity = nproPurchRequestHeaderService.selectOne(
                new EntityWrapper<NproPurchRequestHeaderEntity>()
                        .eq("purch_request_code", requestCode));
        if (headerEntity == null || StringUtils.isBlank(headerEntity.getOAccount())) {
            return "下单人OA账号为空！";
        }
        return null;
    }

    /**
     * 调用服务，生成客户端的服务代理
     *
     * @param address      WebService的URL
     * @param serviceClass 服务接口全名
     * @return 服务代理对象
     * @throws Exception
     */
    private static Object callService(String address, Class serviceClass) {

        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

        // 记录入站消息
        factory.getInInterceptors().add(new LoggingInInterceptor());

        // 记录出站消息
        factory.getOutInterceptors().add(new LoggingOutInterceptor());


        factory.setServiceClass(serviceClass);
        factory.setAddress(address);

        // 创建服务代理并返回
        return factory.create();
    }

}
